/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2021-03-24 23:49:18
 * @LastEditors: sueRimn
 * @LastEditTime: 2021-04-01 01:28:15
 */
// 实现这个项目的构建任务
const {
  src,
  dest,
  parallel,
  series,
  watch
} = require('gulp');

// css 转换插件
const sass = require('gulp-sass');

// js 转换插件
const babel = require('gulp-babel');

// 模板页面转换
const swig = require('gulp-swig');
const data = {
  menus: [{
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [{
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
// 图片文字压缩
const imagemin = require('gulp-imagemin');

// css js html 文件压缩
const cleanCss = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const htmlmin = require('gulp-htmlmin');
const gulpIf = require('gulp-if')

// 清除插件
const del = require('del')

// 开发服务器插件
const browserSync = require('browser-sync')
const bs = browserSync.create()

const useref = require('gulp-useref')

// js任务
const script = () => {
  return src('src/assets/scripts/*.js', {
      base: 'src'
    })
    .pipe(babel({
      presets: ['@babel/preset-env']
    }))
    .pipe(dest('temp'))
}

// 样式任务
const style = () => {
  return src('src/assets/styles/*.scss', {
    base: 'src'
  }).pipe(sass({
    outputStyle: 'expanded' // 完全展开形式
  })).pipe(dest('temp'))
}

//页面任务
const page = () => {
  return src('src/*.html', {
      base: 'src'
    })
    .pipe(swig({
      data
    }))
    .pipe(dest('temp'))
}

// 图片任务
const image = () => {
  return src('src/assets/images/**', {
      base: 'src'
    })
    .pipe(imagemin())
    .pipe(dest('dist'))
}

// 文字任务
const font = () => {
  return src('src/assets/fonts/**', {
      base: 'src'
    })
    .pipe(imagemin())
    .pipe(dest('dist'))
}

// 额外文件任务
const extra = () => {
  return src('public/**', {
      base: 'public'
    })
    .pipe(dest('dist'))
}

// 清除任务
const clean = () => {
  return del(['dist', 'temp'])
}

// 开发服务器任务
const serveTask = () => {
  // 源代码更新 直接更新服务器
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  // watch('src/*.html', page)
  // watch('src/assets/images/**', image)
  // watch('src/assets/fonts/**', font)
  //自动重新加载
  watch(['src/*.html', 'src/assets/images/**', 'src/assets/fonts/**'], bs.reload)
  bs.init({
    notify: false,
    port: 8001,
    files: 'temp/**', // 可以在每个任务 pipe.(bs.reload({stream: true})) 来替代
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules', // dis的文件引用 但是线上这样做不可以 ，可以通过useref 打包一个把需要用到node_modules文件打包到dist那里去
      }
    }
  })
}

// 文件引入任务 对dist里的html文件里的引用文件，做一个处理然后放在dist那里
const userefTask = () => {
  return src('temp/*.html', {
      base: 'temp'
    }).pipe(useref({
      searchPath: ['temp', '.']
    }))
    // 文件压缩 css js html 
    .pipe(gulpIf(/\.js$/, uglify()))
    .pipe(gulpIf(/\.css$/, cleanCss()))
    .pipe(gulpIf(/\.html$/, htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
    })))
    .pipe(dest('dist'))
}
// 需要编译script, style, page
const compile = parallel(script, style, page);
// 打包 先清除旧文件,然后在编译，再改变特殊文件路径
const build = series(clean, parallel(series(compile, userefTask), extra, image, font));
// 启动Server前要 编译一下
const serve = series(compile, serveTask)

module.exports = {
  build,
  compile,
  serve,
}
